loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
local xres,yres = (sw/1920), (sh/1080)
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )
font2 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 20*px )
font3 = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 16*px )

cmtaPANELskrzynek = false
page = 1
caseID = 0
ruletkapola = dxCreateRenderTarget(xres*800, yres*150, true)
time = math.random(5000,9000)
caseinfo = {}

cases = {
    ["Skrzynia Różności"] = {
        name = "#DD6B20Skrzynia Różności";
        awards= {
        [1] = {"Punkty","img/point.png",50,150,0,4000},
        [2] = {"Exp","img/exp.png",20,60,4000,8000},
        [3] = {"Tytoń","img/cigarette.png",1,7,8000,9500},
        [4] = {"Marihuana","img/marihuana.png",1,5,9500,10001},
        };
    };
    ["Skrzynia Ryzyka"] = {
        name = "#DD6B20Skrzynia Ryzyka";
        awards= {
        [1] = {"Punkty","img/point.png",3000,5000,0,5000},
        [2] = {"Ban na 3 dni","img/ban.png",3,3,5000,10001},
        };
    };
}

addEvent("cmta_cases:showGUI", true)
addEventHandler("cmta_cases:showGUI", resourceRoot, function(result)
cmtaPANELskrzynek = true
caseID = 0
resultinfo = result
rndRect = dgsCreateRoundRect(0,false,tocolor(30,30,30,230))  
rect = dgsCreateImage(350*px,150*py,666*px,468*py,rndRect,false)
text = dgsCreateLabel(0*px,20*py,666*px,50*py,"#DD6B20Skrzynie",false,rect)
dgsSetFont( text, font2 )
dgsSetProperty(text,"colorcoded",true)
dgsSetProperty(text,"alignment",{"center","top"})
buybutton1 = dgsCreateButton( 70*px, 320*py, 128*px, 30*py, "Zobacz szczegóły", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
buybutton2 = dgsCreateButton( 270*px, 320*py, 128*px, 30*py, "Zobacz szczegóły", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
buybutton3 = dgsCreateButton( 470*px, 320*py, 128*px, 30*py, "Zobacz szczegóły", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
buttonexit = dgsCreateButton( 630*px, 20*py, 20*px, 20*py, "✖", false,rect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0),tocolor(255,0,0))
button1 = dgsCreateButton( 283*px, 420*py, 30*px, 30*py, " 1", false,rect,false,false,false,false,false,false,tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32))
button2 = dgsCreateButton( 318*px, 420*py, 30*px, 30*py, " 2", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
button3 = dgsCreateButton( 353*px, 420*py, 30*px, 30*py, " 3", false,rect,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32))
image1 = dgsCreateImage(70*px,80*py,128*px,128*py,"img/chest1.png",false,rect,tocolor(255,255,255,255))
image2 = dgsCreateImage(270*px,80*py,128*px,128*py,"img/chest2.png",false,rect,tocolor(255,255,255,255))
image3 = dgsCreateImage(470*px,80*py,128*px,128*py,"img/chest3.png",false,rect,tocolor(255,255,255,255))
textimg1 = dgsCreateLabel(70*px,230*py,128*px,50*py,""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..result[1]["key1"].."",false,rect)
textimg2 = dgsCreateLabel(270*px,230*py,128*px,50*py,""..cases["Skrzynia Ryzyka"].name.."\n#808080Klucze: "..result[1]["key5"].."",false,rect)
textimg3 = dgsCreateLabel(470*px,230*py,128*px,50*py,""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..result[1]["key3"].."",false,rect)
dgsSetFont( textimg1, font )
dgsSetProperty(textimg1,"colorcoded",true)
dgsSetProperty(textimg1,"alignment",{"center","top"})
dgsSetFont( textimg2, font )
dgsSetProperty(textimg2,"colorcoded",true)
dgsSetProperty(textimg2,"alignment",{"center","top"})
dgsSetFont( textimg3, font )
dgsSetProperty(textimg3,"colorcoded",true)
dgsSetProperty(textimg3,"alignment",{"center","top"})
showCursor(true)
addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
end)

function createcaseinfo()
    dxDrawRoundedRectangle(xres*360, yres*280, xres*1200.0, yres*150,15,tocolor(0,0,0,220))
end

function dxDrawRoundedRectangle(x, y, width, height, radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+radius, width-(radius*2), height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawCircle(x+radius, y+radius, radius, 180, 270, color, color, 16, 1, postGUI)
    dxDrawCircle(x+radius, (y+height)-radius, radius, 90, 180, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, (y+height)-radius, radius, 0, 90, color, color, 16, 1, postGUI)
    dxDrawCircle((x+width)-radius, y+radius, radius, 270, 360, color, color, 16, 1, postGUI)
    dxDrawRectangle(x, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y+height-radius, width-(radius*2), radius, color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+width-radius, y+radius, radius, height-(radius*2), color, postGUI, subPixelPositioning)
    dxDrawRectangle(x+radius, y, width-(radius*2), radius, color, postGUI, subPixelPositioning)
end

function detailsgui(info)
    detailrndRect = dgsCreateRoundRect({{20,false},{20,false},{20,false},{20,false}},tocolor(30,30,30,230))  
    detailrect = dgsCreateImage(259*px,330*py,566*px,268*py,detailrndRect,false)
    openbutton = dgsCreateButton( 75*px, 210*py, 158*px, 40*py, "Otwórz skrzynkę", false,detailrect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
    closebutton = dgsCreateButton( 333*px, 210*py, 158*px, 40*py, "Wróć", false,detailrect,false,false,false,false,false,false,tocolor(90,90,90),tocolor(221,107,32),tocolor(221,107,32))
    dgsSetFont( openbutton, font )
    dgsSetFont( closebutton, font )
    text = dgsCreateLabel(0*px,20*py,566*px,50*py,info,false,detailrect)
    dgsSetFont( text, font )
    dgsSetProperty(text,"colorcoded",true)
    dgsSetProperty(text,"alignment",{"center","top"})
    mygrid = dgsCreateGridList (850*px, 330*py, 260*px, 268*py, false )
    dgsGridListAddColumn( mygrid, "", 0.2 )
    dgsGridListAddColumn( mygrid, "", 0.5 ) 
    dgsGridListAddColumn( mygrid, "", 0.2 ) 
    dgsGridListSetAutoSortEnabled(mygrid,false)
    dgsGridListSetSortEnabled(mygrid,false)
    dgsSetProperty(mygrid,"rowHeight",36*py)
    dgsSetProperty(mygrid, "font",font )
end

function onclick ( btn, state )
    if btn == "left" and state == "down" and cmtaPANELskrzynek == true then
        if source == button1 or source == button2 or source == button3 then 
            refreshcolors()
        end
        if source == button1 then
        dgsSetProperty(button1,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
        dgsImageSetImage(image1,"img/chest1.png")
        dgsImageSetImage(image2,"img/chest2.png")
        dgsImageSetImage(image3,"img/chest3.png")
        dgsSetText( textimg1, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key1"].."" )
        dgsSetText( textimg2, ""..cases["Skrzynia Ryzyka"].name.."\n#808080Klucze: "..resultinfo[1]["key5"].."" )
        dgsSetText( textimg3, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key3"].."" )
        page = 1
        end
        if source == button2 then
            dgsSetProperty(button2,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/chest1.png")
            dgsImageSetImage(image2,"img/chest2.png")
            dgsImageSetImage(image3,"img/chest3.png")
            dgsSetText( textimg1, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key1"].."" )
            dgsSetText( textimg2, ""..cases["Skrzynia Ryzyka"].name.."\n#808080Klucze: "..resultinfo[1]["key5"].."" )
            dgsSetText( textimg3, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key3"].."" )
            page = 2
        end
        if source == button3 then
            dgsSetProperty(button3,"color",{tocolor(221,107,32),tocolor(221,107,32),tocolor(221,107,32)})
            dgsImageSetImage(image1,"img/chest1.png")
            dgsImageSetImage(image2,"img/chest2.png")
            dgsImageSetImage(image3,"img/chest3.png")
            dgsSetText( textimg1, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key1"].."" )
            dgsSetText( textimg2, ""..cases["Skrzynia Ryzyka"].name.."\n#808080Klucze: "..resultinfo[1]["key5"].."" )
            dgsSetText( textimg3, ""..cases["Skrzynia Różności"].name.."\n#808080Klucze: "..resultinfo[1]["key3"].."" )
            page = 3
        end  
        
        if source == buybutton1 then
            dgsSetEnabled(rect,false)
            dgsSetAlpha(rect,0)
            local info = ""..cases["Skrzynia Różności"].name.."\n\n#808080Aby otworzyć tę skrzynkę potrzebujesz\n#00ff001x Klucz do Skrzyni Różności\n\n#ccc000ℹ️ Klucz możesz zdobyć poprzez klikanie w panelu pod przyciskiem F2\n bądź zakupić go w sklepie https://sklep.clickermta.pl/\n\n#ff0000⚠️ Nie opuszczaj serwera, jeśli losowanie jest w toku."
            detailsgui(info)
            createLootTemplate(1)
            caseID = 1
            addEventHandler ( "onClientRender", getRootElement(), createcaseinfo)
        end


        if source == buybutton2 then
            dgsSetEnabled(rect,false)
            dgsSetAlpha(rect,0)
            local info = ""..cases["Skrzynia Ryzyka"].name.."\n\n#808080Aby otworzyć tę skrzynkę potrzebujesz\n#00ff001x Klucz do Skrzyni Ryzyka\n\n#ccc000ℹ️ Klucz możesz zdobyć poprzez klikanie w panelu pod przyciskiem F2\n bądź zakupić go w sklepie https://sklep.clickermta.pl/\n\n#ff0000⚠️ Nie opuszczaj serwera, jeśli losowanie jest w toku."
            detailsgui(info)
            createLootTemplate(2)
            caseID = 2
            addEventHandler ( "onClientRender", getRootElement(), createcaseinfo)
        end

        if source == openbutton then
            triggerServerEvent("cmta_cases:KeyCheck", resourceRoot,caseID) 
            dgsSetEnabled(openbutton,false)
            dgsSetEnabled(closebutton,false)
        end
        if source == closebutton then
            destroyElement(detailrndRect)
            destroyElement(detailrect)
            destroyElement(mygrid)
            removeEventHandler ( "onClientRender", getRootElement(), createcaseinfo)
            destroyLootTemplate()
            dgsSetEnabled(rect,true)
            dgsSetAlpha(rect,1)
        end
        if source == buttonexit then
            removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
            destroyElement(rndRect)
            destroyElement(rect)
            showCursor(false)
            cmtaPANELskrzynek = false
        end

    end
end

addEvent("cmta_cases:roll", true)
addEventHandler("cmta_cases:roll", resourceRoot, function(result)
readyroll(caseID)
end)

addEvent("cmta_cases:refreshcases", true)
addEventHandler("cmta_cases:refreshcases", resourceRoot, function(result)
readyroll(caseID)
end)

function refreshcolors()
    dgsSetProperty(button1,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button2,"color",{tocolor(90,90,90),tocolor(221,107,32)})
    dgsSetProperty(button3,"color",{tocolor(90,90,90),tocolor(221,107,32)})
end


function destroyLootTemplate()
if isElement(case1img) then destroyElement(case1img) end
if isElement(case2img) then destroyElement(case2img) end
if isElement(case3img) then destroyElement(case3img) end
if isElement(case4img) then destroyElement(case4img) end
end



function createLootTemplate(id)
if id == 1 then
    case1img = dxCreateTexture( cases["Skrzynia Różności"].awards[1][2] ,"argb", false, "clamp" )
    case2img = dxCreateTexture( cases["Skrzynia Różności"].awards[2][2] ,"argb", false, "clamp" )
    case3img = dxCreateTexture( cases["Skrzynia Różności"].awards[3][2] ,"argb", false, "clamp" )
    case4img = dxCreateTexture( cases["Skrzynia Różności"].awards[4][2] ,"argb", false, "clamp" )

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case1img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Różności"].awards[1] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..((cases["Skrzynia Różności"].awards[1][6]-cases["Skrzynia Różności"].awards[1][5])/100).."%")

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case2img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Różności"].awards[2] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..((cases["Skrzynia Różności"].awards[2][6]-cases["Skrzynia Różności"].awards[2][5])/100).."%")

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case3img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Różności"].awards[3] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..((cases["Skrzynia Różności"].awards[3][6]-cases["Skrzynia Różności"].awards[3][5])/100).."%")

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case4img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Różności"].awards[4] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..(((cases["Skrzynia Różności"].awards[4][6]-cases["Skrzynia Różności"].awards[4][5])/100)-0.01).."%")
elseif id == 2 then
    case1img = dxCreateTexture( cases["Skrzynia Ryzyka"].awards[1][2] ,"argb", false, "clamp" )
    case2img = dxCreateTexture( cases["Skrzynia Ryzyka"].awards[2][2] ,"argb", false, "clamp" )

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case1img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Ryzyka"].awards[1] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..((cases["Skrzynia Ryzyka"].awards[1][6]-cases["Skrzynia Ryzyka"].awards[1][5])/100).."%")

    local row = dgsGridListAddRow ( mygrid )
    dgsGridListSetItemImage( mygrid, row, 1, case2img,tocolor(255,255,255,255),0*px,3*py,32*px,32*py,false)
    dgsGridListSetItemText ( mygrid, row, 2, cases["Skrzynia Ryzyka"].awards[2] )
    dgsGridListSetItemText ( mygrid, row, 3, ""..(((cases["Skrzynia Ryzyka"].awards[2][6]-cases["Skrzynia Ryzyka"].awards[2][5])/100)-0.01).."%")

end

end






function rollSkrzyniaRoznosci()
    draw = {}
    for i=1,48 do
        local rn = math.random(0,10000)
        local case = cases["Skrzynia Różności"].awards
        if rn >= case[1][5] and rn < case[1][6] then
            table.insert(draw,{"Skrzynia Różności",case[1][1],case[1][2],math.random(case[1][3],case[1][4])})
        elseif rn >= case[2][5] and rn < case[2][6] then
            table.insert(draw,{"Skrzynia Różności",case[2][1],case[2][2],math.random(case[2][3],case[2][4])})
        elseif rn >= case[3][5] and rn < case[3][6] then
            table.insert(draw,{"Skrzynia Różności",case[3][1],case[3][2],math.random(case[3][3],case[3][4])})
        elseif rn >= case[4][5] and rn < case[4][6] then
            table.insert(draw,{"Skrzynia Różności",case[4][1],case[4][2],math.random(case[4][3],case[4][4])})
        end
    end
end

function rollSkrzyniaRyzyka()
    draw = {}
    for i=1,48 do
        local rn = math.random(0,10000)
        local case = cases["Skrzynia Ryzyka"].awards
        if rn >= case[1][5] and rn < case[1][6] then
            table.insert(draw,{"Skrzynia Ryzyka",case[1][1],case[1][2],math.random(case[1][3],case[1][4])})
        elseif rn >= case[2][5] and rn < case[2][6] then
            table.insert(draw,{"Skrzynia Ryzyka",case[2][1],case[2][2],math.random(case[2][3],case[2][4])})
        end
    end
end



function drawgui()

    local postep =  (getTickCount()- start)/(koniec- start)
    przesuniecie = interpolateBetween(-15*70,0,0,yres*-random*150,0,0,postep ,'OutQuad')
    x=70*6.21
    dxSetRenderTarget()
    dxSetRenderTarget(ruletkapola,true)
    for i,v in ipairs(draw) do
        dxDrawImage(xres*x+przesuniecie, yres*0, xres*150.0, yres*150, v[3])
        x=x+150
    end
    dxSetRenderTarget()
    dxDrawImage(xres*360, yres*280, xres*1200.0, yres*150, ruletkapola)
    dxDrawRectangle(xres*955, yres*280, xres*10, yres*150,tocolor(255, 0,0, 200))
end

function readyroll(id)
    if id == 1 then
    rollSkrzyniaRoznosci()
    elseif id == 2 then
    rollSkrzyniaRyzyka()
    end
    random = math.random(35,45)
    start = getTickCount()
    koniec = getTickCount()+time
    addEventHandler("onClientRender", getRootElement(), drawgui )
    if draw[random][2] == "Ban na 3 dni" then
        triggerServerEvent("cmta_cases:addBan", resourceRoot)
    end
    setTimer(function()
        if draw[random][2] == "Ban na 3 dni" then
        triggerServerEvent("cmta_cases:addBan", resourceRoot,"kick")
        end
        outputChatBox("#00ff00[SKRZYNKA] #808080Otrzymujesz #ff0000"..draw[random][2]..".",255,255,255,true)
        
        case_effect = dgsCreateEffect3D(20)
        casesp = dgsCreateScrollPane(450*px,200*py,466*px,368*py,false,false)  
        caseimg = dgsCreateImage(0,0,1,1,_,true,casesp,tocolor(0,0,0,160))
        dgsEffect3DApplyToScrollPane(case_effect,casesp)
        award_text1 = dgsCreateLabel(0*px,10*py,466*px,20*py,"#ff0000GRATULACJE!",false,caseimg)
        dgsSetFont( award_text1, font2 )
        dgsSetProperty(award_text1,"colorcoded",true)
        dgsSetProperty(award_text1,"alignment",{"center","center"})
        award_text2 = dgsCreateLabel(0*px,290*py,466*px,20*py,"#808080Twoja nagroda to:\n#ff2929"..draw[random][4].." "..draw[random][2].."",false,caseimg)
        dgsSetFont( award_text2, font3 )
        dgsSetProperty(award_text2,"colorcoded",true)
        dgsSetProperty(award_text2,"alignment",{"center","center"})
        award_img = dxCreateTexture( draw[random][3] ,"argb", false, "clamp" )
        award_img2 = dgsCreateImage(100*px,21*py,266*px,266*py,award_img,false,caseimg)
        playSound("img/sound.mp3")



        setTimer(function()
            removeEventHandler("onClientRender", getRootElement(), drawgui )
            setTimer(function()
            dgsSetEnabled(openbutton,true)
            dgsSetEnabled(closebutton,true)
            destroyElement(case_effect)
            destroyElement(casesp)
            destroyElement(award_img)
            end,3000,1)
        end,3000,1)

    end,time,1)

    
end

